package com.jwthhealth_pub.old.report.view;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.report.base.ReprotBaseActivity;
import com.jwthhealth_pub.old.report.model.resbody.HraDateRes;
import com.jwthhealth_pub.old.report.presenter.ReportPresenter;
import com.jwthhealth_pub.old.report.view.fragment.ReportContentFragment;
import com.jwthhealth_pub.old.report.view.fragment.TransitionFragment;

import java.util.List;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

public class NewReportInfoActivity extends ReprotBaseActivity {

    private static final String TAG = makeLogTag(NewReportInfoActivity.class);

    private static final String CONTENTTAG = "content";
    private static final String TRANSITIONTAG = "transition";

    private ReportContentFragment mContentFragment;
    private TransitionFragment mTransitionFragment;
    private List<HraDateRes.DataBean> mDateList;
    private ReportPresenter mPresenter;

    public ReportPresenter getmPresenter() {
        return mPresenter;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_reprot_info);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle(getResources().getString(R.string.app_name));
        }

        if (savedInstanceState == null) {
            loadFragments();
        } else {
            FragmentManager fm = getSupportFragmentManager();
            mContentFragment = (ReportContentFragment) fm.findFragmentByTag(CONTENTTAG);
            FragmentTransaction ft = fm.beginTransaction();
            ft.show(mContentFragment);
            ft.commit();
        }

        if (mContentFragment != null) {
            mPresenter = new ReportPresenter(mContentFragment);
            mDateList = getIntent().getParcelableArrayListExtra("datalist");
            if (mDateList == null) return;
            mPresenter.setmUserDateList(mDateList);
            String id = mDateList.get(0).getNid();
            mPresenter.loadDate(mDateList.get(0));
        }
    }

    @Override
    protected void title() {
        TitleLayout titleLayout = (TitleLayout) findViewById(R.id.title_layout);
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    public void loadFragments() {
        mContentFragment = new ReportContentFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("datalist", (Parcelable) mDateList);
        mContentFragment.setArguments(bundle);
        mTransitionFragment = new TransitionFragment();
        // TODO: 2017/8/4 抽取ActivityUtil
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.container, mContentFragment, CONTENTTAG);
        ft.add(R.id.container, mTransitionFragment, TRANSITIONTAG);
        ft.hide(mContentFragment);
        ft.show(mTransitionFragment);
        ft.commit();
    }

    public void showContentFragment() {
        if (mContentFragment != null && mTransitionFragment != null) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.hide(mTransitionFragment);
            ft.show(mContentFragment);
            ft.commit();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void showTransitionUi(String msg) {
        if (mTransitionFragment != null) {
            mTransitionFragment.showTransitionMsg(msg);
        }
    }
}
